package com.jc.auth.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.jc.auth.common.enums.IsDeletedFlagEnum;
import com.jc.auth.domain.convert.AuthPermissionBOConverter;
import com.jc.auth.domain.convert.AuthRolePermissionBOConverter;
import com.jc.auth.domain.entity.AuthPermissionBO;
import com.jc.auth.domain.entity.AuthRolePermissionBO;
import com.jc.auth.domain.service.AuthPermissionDomainService;
import com.jc.auth.domain.service.AuthRolePermissionDomainService;
import com.jc.auth.infra.basic.entity.AuthPermission;
import com.jc.auth.infra.basic.entity.AuthRolePermission;
import com.jc.auth.infra.basic.service.AuthPermissionService;
import com.jc.auth.infra.basic.service.AuthRolePermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @Description:
 * @Author: ASL_ly
 * @Package: com.jc.auth.domain.service.impl
 * @Project: jc-club
 * @Date: 2024/10/29  13:04
 */
@Service
@Slf4j
public class AuthRolePermissionDomainServiceImpl implements AuthRolePermissionDomainService {
    @Resource
    private AuthRolePermissionService authRolePermissionService;

    /**
     * 新增角色权限关联
     * @param authRolePermissionBO
     * @return
     */
    @Override
    public Boolean add(AuthRolePermissionBO authRolePermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("rolePermissionController.add.bo:{}", JSON.toJSONString(authRolePermissionBO));
        }
        List<Long> permissionIdList = authRolePermissionBO.getPermissionIdList();
        Long roleId = authRolePermissionBO.getRoleId();
        List<AuthRolePermission> authRolePermissionList = new LinkedList<>();
        permissionIdList.forEach(permissionId ->{
            AuthRolePermission authRolePermission = AuthRolePermission.builder()
                    .roleId(roleId)
                    .permissionId(permissionId)
                    .isDeleted(IsDeletedFlagEnum.UN_DELETED.code)
                    .build();
            authRolePermissionList.add(authRolePermission);
        });
        Integer count = authRolePermissionService.batchInsert(authRolePermissionList);
        return count > 0;
    }


}
